﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfeccionesJR.BussinesLogic;
using ConfeccionesJR.Entites;
using Microsoft.Practices.Unity;

namespace ConfeccionesJR.UI
{
    public partial class frmPagos : Form
    {
        [Dependency]
        public IClienteBL clienteBL { get; set; }

        [Dependency]
        public IPagoBL pagoBL { get; set; }

        [Dependency]
        public ICreditoBL creditoBL { get; set; }

        public frmPagos()
        {
            InitializeComponent();
            this.BuildUp();
        }
        
        private void frmPagos_Load(object sender, EventArgs e)
        {
        }
        
        bool isNew = true;
        
        public void nuevoPago(int creditoId, int clienteId)
        {
            cLIENTEBindingSource.DataSource = clienteBL.GetFromClienteById(clienteId);
            cREDITOBindingSource.DataSource = creditoBL.GetFromCreditoById(creditoId);
            dETALLEPAGOBindingSource.AddNew();

            var currentCredito = cREDITOBindingSource.Current as CREDITO;
            if (currentCredito.DETALLEPAGOes.Count > 0)
            {
                txtFecUltPago.Text = currentCredito.DETALLEPAGOes.Last().Fecha.ToShortDateString();
                txtMonto.Text = currentCredito.DETALLEPAGOes.Last().Monto.ToString();
            }
            var fechaCredito = (creditoBL.GetFromCreditoById(creditoId)).FechaCredito.Date;
            txtFecVen.Text = fechaCredito.AddDays(30).ToShortDateString();

        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            var currentPago = dETALLEPAGOBindingSource.Current as DETALLEPAGO;
            var currentCredito = cREDITOBindingSource.Current as CREDITO;
            var msg = "ERROR";

            if (currentCredito != null && importeTextBox.Text != "" && importeTextBox.Text != string.Empty)
            {
                if (isNew == true)
                {
                    try
                    {
                        currentPago.CreditoId = currentCredito.Id;
                        currentPago.Descripcion = "Pago hecho a las " + Convert.ToString(DateTime.Now.ToLongTimeString());
                        currentPago.Fecha = DateTime.Today.Date;
                        currentPago.Monto = Convert.ToDecimal(importeTextBox.Text);
                        pagoBL.AddPago(currentPago);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        frmCredito  frm = new frmCredito();
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("Para guardar es necesario ingresar un monto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void importeTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (
                !char.IsNumber(e.KeyChar) && 
                !char.IsDigit(e.KeyChar) && 
                e.KeyChar != (char)Keys.Back && 
                e.KeyChar != (char)Keys.Enter
                )
            {
                e.Handled = true;
            }           

            if(e.KeyChar=='.' || e.KeyChar=='\b')
            {
                e.Handled = false;
            }
        }

        
    }
}
